package tcc.wa.view.adapter;

import java.util.List;

import tcc.wa.R;
import tcc.wa.model.Alert;
import tcc.wa.model.AlertType;
import tcc.wa.presenter.view.item.HoursInAdvanceViewItem;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

/**
 * ArrayAdapter for Alerts.
 */
public class AlertAdapter extends ArrayAdapter<Alert> {
	
	private final Context context;
	
	private final List<Alert> alerts;
	
	public AlertAdapter(Context context, List<Alert> alerts) {
		super(context, R.layout.alert_list_item, alerts);
		this.context = context;
		this.alerts = alerts;
	}
	
	/* (non-Javadoc)
	 * @see android.widget.ArrayAdapter#getView(int, android.view.View, android.view.ViewGroup)
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View rowView = inflater.inflate(R.layout.alert_list_item, parent, false);
		Alert alert = alerts.get(position);
		TextView typeTextView = (TextView) rowView.findViewById(R.id.alertTypeTextViewValue);
		typeTextView.setText(alert.getType().toString());
		TextView valueTextView = (TextView) rowView.findViewById(R.id.alertValueTextView);
		TextView alertValueValueTextView = (TextView) rowView.findViewById(R.id.alertValueTextViewValue);
		setValueText(valueTextView, alertValueValueTextView, alert.getType(), alert.getValue());
		TextView hoursInAdvanceTextView = (TextView) rowView.findViewById(R.id.alertHoursInAdvanceTextViewValue);
		hoursInAdvanceTextView.setText(new HoursInAdvanceViewItem(alert.getHoursInAdvance()).toString());
		return rowView;
	}
	
	/**
	 * Sets the value text.
	 *
	 * @param valueTextView the value text view
	 * @param alertValueTextView the alert value text view
	 * @param type the Alert type
	 * @param value the Alert value
	 */
	private void setValueText(TextView valueTextView, TextView alertValueTextView, AlertType type, double value) {
		switch (type) {
		case HIGH_TEMPERATURE:
			valueTextView.setText(context.getResources().getString(R.string.above));
			alertValueTextView.setText(String.format("%s %s", value, context.getResources().getString(R.string.celsius_degree)));
			break;
		case LOW_TEMPERATURE:
			valueTextView.setText(context.getResources().getString(R.string.below));
			alertValueTextView.setText(String.format("%s %s", value, context.getResources().getString(R.string.celsius_degree)));
			break;
		case RAIN:
			valueTextView.setText(context.getResources().getString(R.string.above));
			alertValueTextView.setText(String.format("%s %s", value, context.getResources().getString(R.string.milimeter)));
			break;
		default:
			valueTextView.setText(context.getResources().getString(R.string.above));
			alertValueTextView.setText(String.format("%s %s", value, context.getResources().getString(R.string.celsius_degree)));
		}
	}
}
